package com.imooc.articles_mongodb_project.controller;

import com.imooc.articles_mongodb_project.common.Result;
import com.imooc.articles_mongodb_project.common.validator.SaveValidator;
import com.imooc.articles_mongodb_project.common.validator.UpdateValidator;
import com.imooc.articles_mongodb_project.dto.ArticleDTO;
import com.imooc.articles_mongodb_project.query.ArticleQuery;
import com.imooc.articles_mongodb_project.service.IArticlesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "文章管理接口")
@RestController
@RequestMapping("/articles")
public class ArticlesController {

    @Autowired
    private IArticlesService articlesService;

    @ApiOperation(value = "新增文章数据")
    @PostMapping("/save")
    public Result save(@RequestBody @Validated(SaveValidator.class) ArticleDTO articleDTO) {
        return articlesService.save(articleDTO);
    }

    @ApiOperation(value = "根据文章id获取文章对象")
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") String id) {
        return articlesService.getById(id);
    }


    @GetMapping("/list")
    public Result list() {
        return articlesService.list();
    }

    @ApiOperation(value = "条件查询文章内容")
    @PostMapping("/listByParams")
    public Result list(@RequestBody ArticleQuery articleQuery) {
        return articlesService.listByParams(articleQuery);
    }

    @ApiOperation(value = "根据id更新文章数据")
    @PutMapping("/update")
    public  Result update(@RequestBody @Validated(UpdateValidator.class) ArticleDTO articleDTO){
        return articlesService.update(articleDTO);
    }

    @ApiOperation(value = "根据文章id删除文章数据")
    @DeleteMapping("/deleteOne/{id}")
    public Result deleteOne(@PathVariable("id") String id){
        return articlesService.deleteOne(id);
    }


}
